package com.wangzhixuan.commons.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Map;

/**
 * @author lixin
 */
@Getter
@Setter
public class PageInfo<T> {
    /**
     * 默认显示的记录数
     */
    private final static int PAGESIZE = 10;
    /**
     * 总记录
     */
    private long total;
    /**
     * 显示的记录
     */
    private List<T> rows;

    @JsonIgnore
    private int from;
    @JsonIgnore
    private int size;
    /**
     * 当前页
     */
    @JsonIgnore
    private int nowpage;
    /**
     * 每页显示的记录数
     */
    @JsonIgnore
    private int pagesize;
    /**
     * 查询条件
     */
    @JsonIgnore
    private Map<String, Object> condition;
    /**
     * 排序字段
     */
    @JsonIgnore
    private String sort = "seq";
    /**
     * asc，desc mybatis Order 关键字
     */
    @JsonIgnore
    private String order = "asc";

    public PageInfo() {
    }

    //构造方法
    public PageInfo(int nowpage, int pagesize) {
        //计算当前页  
        if (nowpage < 0) {
            this.nowpage = 1;
        } else {
            //当前页
            this.nowpage = nowpage;
        }
        //记录每页显示的记录数  
        if (pagesize < 0) {
            this.pagesize = PAGESIZE;
        } else {
            this.pagesize = pagesize;
        }
        //计算开始的记录和结束的记录  
        this.from = (this.nowpage - 1) * this.pagesize;
        this.size = this.pagesize;
    }

    // 构造方法
    public PageInfo(int nowpage, int pagesize, String sort, String order) {
        this(nowpage, pagesize);
        // 排序字段，正序还是反序
        this.sort = sort;
        this.order = order;
    }
}
